var page = 0;
var current = 1;
var recStatus = 0;
$(document).ready(function() {
	postReq(current, recStatus);
	var menu_id = "#status_menu";
	var item_id = "#status_item a";
	var pre_id = "#pre_page";
	var next_id = "#next_page";
	clickStatusItem(item_id, menu_id);
	clickPN(pre_id, next_id, menu_id);
});

function postReq(current, recStatus) {
	$.ajax({
		url : webRoot + "client/rec/info",
		type : "post",
		dataType : "json",
		data : {
			"current" : current,
			"recStatus" : recStatus
		},
		success : function(data) {
			page = data.page;
			// alert(data.recList);
			initPageItem("#info_page", "#page_menu")
			createPN("#pre_page", "#next_page", page, current);
			var tbody = $("#list_table tbody");
			tbody.empty();
			$.each(data.recList, function(index, value) {
				tbody.append("<tr></tr>");
				var tr = tbody.children("tr:last");
				tr.append("<td>" + value.buName + "</td>");
				tr.append("<td>" + value.crNo + "</td>");
				tr.append("<td>" + value.deskNo + "</td>");
				tr.append("<td>" + value.recDate + "</td>");
				var sta;
				var opera = "取消";
				if (value.recStatus == 1) {
					sta = "已预约未入座";
				} else if (value.recStatus == 2) {
					sta = "已预约已入座";
				} else if (value.recStatus == 3) {
					sta = "已取消预约";
					opera = "";
				}
				tr.append("<td>" + sta + "</td>");
				tr.append("<td><a href='#' onclick='cancel(" + value.recId
						+ ")'>"+opera+"</a></td>");
			});
			initNP("#pre_page","#next_page");
		}
	});
}

function initNP(pre_id, next_id) {
	var doc_pre = $(pre_id);
	var doc_next = $(next_id);
	var pre_p, next_p;
	pre_p = (current - 1) > 0 ? (current - 1) : current;
	next_p = (current + 1) <= page ? (current + 1) : current;
	doc_pre.attr("role",pre_p);
	doc_next.attr("role",next_p);
}

function clickStatusItem(item_id, menu_id) {
	var items = $(item_id);
	var menu = $(menu_id);
	for (var i = 0; i < items.length; i++) {
		items.eq(i).click(function() {
			// alert($(this).text());
			recStatus = $(this).attr("role");
			current = 1;
			page = 0;
			alert(recStatus);
			postReq(current, recStatus);
			menu.attr("role", recStatus);
			menu.text($(this).text());
		});
	}
}

function clickPN(pre_id, next_id, status_id) {
	var doc_pre = $(pre_id);
	var doc_next = $(next_id);
	doc_pre.click(function() {
		current = $(this).attr("role");
		if (current == 1) {
			alert("当前页是第一页！");
		} else {
			postReq(current, recStatus);
		}
	});
	doc_next.click(function() {
		current = $(this).attr("role");
		if (current == page) {
			alert("当前页是最后一页！");
		} else {
			postReq(current, recStatus);
		}
	});
}

function initPageItem(page_id,page_menu){
	var doc_page = $(page_id);
	var doc_menu = $(page_menu);
	doc_menu.html("第 "+current+" 页<span class='caret'></span>");
	createPage(page_id, page, current);
}

function clickPageItem(page_id,page_menu){
	var doc_page = $(page_id);
	var doc_menu = $(page_menu);
	for(var i = 0 ; i < doc_page ; i++){
		doc_page.eq(i).click(function(){
			current = $(this).attr("role");
			postReq(current, recStatus);
		});
	}
}

function cancel(recId){
	$.ajax({
		url:webRoot+"client/rec/cancel?recId="+recId,
		type:"get",
		dataType:"json",
		success:function(data){
			if(data.result == "success"){
				alert("修改成功");
				postReq(current, recStatus);
			}else{
				alert("修改失败");
			}
		}
	});
}
